package com.ehotting.mqbd.core.mq.batch;

import com.ehotting.eaf.core.utils.AssertUtils;
import com.ehotting.eaf.core.utils.SpringUtils;
import com.ehotting.eaf.core.utils.StringUtils;
import com.ehotting.mqbd.api.bean.MqApplicationContext;
import com.ehotting.mqbd.api.bean.solver.SolverStateBean;
import com.ehotting.mqbd.api.bean.solver.ModuleInfoBean;
import com.ehotting.mqbd.api.enums.DirectionEnum;
import com.ehotting.mqbd.api.enums.SolveStepEnum;
import com.ehotting.mqbd.api.enums.TopicEnum;
import com.ehotting.mqbd.core.logger.MqMsgLogger;
import com.ehotting.mqbd.core.manager.ModuleManager;
import com.ehotting.mqbd.core.manager.SolverAllocator;
import com.ehotting.mqbd.core.manager.SolverInfoManager;
import com.ehotting.mqbd.core.call.CallerAble;
import com.ehotting.mqbd.core.mq.consumer.action.ConsumerRequestAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;


public class
BatchConsumerRequestAction {

    private final Logger logger = LoggerFactory.getLogger(BatchConsumerRequestAction.class);

    private List<ConsumerRequestAction> list = new ArrayList<>();

    public BatchConsumerRequestAction(SolverAllocator solverAllocator,
                                      BatchMqApplicationContext  batchMqApplicationContext,
                                      TopicEnum topic,
                                      DirectionEnum direction,
                                      SolverInfoManager solverInfoManager, ModuleManager moduleManager, MqMsgLogger mqMsgLogger){
        List<MqApplicationContext> mqApplicationContextList = batchMqApplicationContext.getMqApplicationContextList();

        logger.info("mqApplicationContextList.size():{}",mqApplicationContextList.size());

        for(MqApplicationContext mqApplicationContext : mqApplicationContextList){
            logger.info("Load module[{}] info[localIp:{},targetIp:{}]",mqApplicationContext.getModule(),mqApplicationContext.getLocalHost(),mqApplicationContext.getConsumerHost());
            ModuleInfoBean protocol = moduleManager.getByModule(mqApplicationContext.getModule());
            logger.info("Module[{}] caller:[{}]",mqApplicationContext.getModule(),protocol.getCaller());
            AssertUtils.notNull(protocol,"module["+mqApplicationContext.getModule()+"] protocol must not be null,please config.");
            AssertUtils.isTrue(StringUtils.hasText(protocol.getCaller()),"module["+protocol.getModule()+"] caller must not be null,please config.");
            CallerAble caller = SpringUtils.getBean(protocol.getCaller());
            AssertUtils.notNull(caller,"module["+protocol.getModule()+"] caller must not be null,please check spring context.");
            if(protocol.getSolveStep() == SolveStepEnum.MULTI){
                List<SolverStateBean> solverStateList = solverInfoManager.getByModule(mqApplicationContext.getModule());
                for(SolverStateBean solverState : solverStateList){
                    String solverIp = solverState.getSolverInfo().getIp();

                    ConsumerRequestAction action = new ConsumerRequestAction(solverAllocator,
                            caller,
                            mqApplicationContext,
                            topic,
                            direction,
                            solverIp,
                            moduleManager,mqMsgLogger);

                    list.add(action);
                }
            }else{
                ConsumerRequestAction action = new ConsumerRequestAction(solverAllocator,
                        caller,
                        mqApplicationContext,
                        topic,
                        direction,
                        null,
                        moduleManager,mqMsgLogger);

                list.add(action);
            }
        }
    }



    public List<ConsumerRequestAction> getList() {
        return list;
    }


}
